<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@include file="/common/taglibs.jspf" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>流程设计</title>
    <meta name="decorator" content="form"/>
</head>
<body>
<div class="layui-fluid">
        <div>
            <button class="layui-btn layui-btn-small" type="button" onclick="treeAdd()"><i class="layui-icon">&#xe61f;</i> 新 增</button>
            <button class="layui-btn layui-btn-danger layui-btn-small" type="button" onclick="treeDel()"><i class="layui-icon">&#xe640;</i>删 除</button>
        </div>
        <div class="layui-row">
            <div class="layui-col-md2" >
                <ul id="busTree" class="ztree"></ul>
            </div>
            <div class="layui-col-md10 layui-col-sm10"  id="treeInfo"  style="display: none">
                <form:form id="busForm" modelAttribute="data"  method="post" class="layui-form" action="/act/bus/edit">
                    <form:hidden path="id"/>
                    <form:hidden path="parentId"/>
                    <form:hidden path="createDate"/>
                    <form:hidden path="createUser"/>
                    <form:hidden path="orgId"/>
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-sm6">
                            <label class="layui-form-label"><font color="red">*</font>父节点名称:</label>
                            <div class="layui-input-block">
                                <form:input path="parentName" class="layui-input" datatype="*" nullmsg="请输入流程名称！" htmlEscape="false" readonly="true" />
                                <label class="Validform_checktip"></label>
                            </div>
                        </div>
                        <div class="layui-col-sm6">
                            <label class="layui-form-label"><font color="red">*</font>名称:</label>
                            <div class="layui-input-block">
                                <form:input path="name" class="layui-input" datatype="*" nullmsg="请输入名称！" htmlEscape="false" />
                                <label class="Validform_checktip"></label>
                            </div>
                        </div>
                    </div>
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-sm6 ">
                            <label class="layui-form-label"><font color="red">*</font>流程KEY:</label>
                            <div class="layui-input-block">
                                <form:input path="actKey" class="layui-input" datatype="*" nullmsg="请输入流程KEY！" htmlEscape="false" />
                                <label class="Validform_checktip"></label>
                            </div>
                        </div>
                        <div class="layui-col-sm6 ">
                            <label class="layui-form-label"><font color="red">*</font>类型:</label>
                            <div class="layui-input-block">
                                <form:select path="type" dict="actBusType" defaultvalue="0"  htmlEscape="false" cssClass="i-checks required" />
                                <label class="Validform_checktip"></label>
                            </div>
                        </div>
                    </div>
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-sm6 ">
                            <label class="layui-form-label"><font color="red">*</font>序号:</label>
                            <div class="layui-input-block">
                                <form:input path="sort" class="layui-input" datatype="*" nullmsg="请输入序号！" htmlEscape="false" />
                                <label class="Validform_checktip"></label>
                            </div>
                        </div>
                        <div class="layui-col-sm6 ">
                            <label class="layui-form-label"><font color="red">*</font>是否打开:</label>
                            <div class="layui-input-block">
                                <form:select path="open" dict="whether" class="form-control " datatype="*" htmlEscape="false" nullmsg="请选择是否打开！">
                                </form:select>
                                <label class="Validform_checktip"></label>
                            </div>
                        </div>
                    </div>
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-sm12">
                            <label class="layui-form-label"><font color="red">*</font>类路径:</label>
                            <div class="layui-input-block">
                                <form:input path="classUrl" class="layui-input" datatype="*" nullmsg="请输入类路径！" htmlEscape="false" />
                                <label class="Validform_checktip"></label>
                            </div>
                        </div>
                    </div>
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-sm12">
                            <label class="layui-form-label">描述:</label>
                            <div class="layui-input-block">
                                <form:textarea path="remark" htmlEscape="false" rows="3" maxlength="200" class="layui-textarea"/>
                            </div>
                        </div>
                    </div>
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-sm12">
                            <div class="layui-input-block">
                                <button class="layui-btn" type="button" id="submitBtn">保 存</button>
                            </div>
                        </div>
                    </div>
                </form:form>
            </div>
        </div>
</div>
<script>
    var setting = {
        view: {dblClickExpand: false}
        ,data: {key: {name:"name"},simpleData: {enable: true,idKey: "id",pIdKey: "parentId"}}
        ,callback: {onClick: zTreeOnClick}
    };
    var ztree;
    $(function(){
        var data='${busTree}';
        ztree=$.fn.zTree.init($("#busTree"), setting, eval(''+data+''));//ztree树加载
    });
    /**
     * 节点单击事件
     */
    function zTreeOnClick(event, treeId, treeNode) {
        var url = "${appPath}/act/bus/info";
        var params ={
            'id':treeNode.id
        }
        $.post(url,params,function (result) {
            if(result.ret == '0'){
                $("#treeInfo").show();
               formload("busForm",result.data);
                layui.form.render();
            }else {
                layer.alert(result.msg);
            }
        });
    };

    /**
     * 新增
     */
    function treeAdd() {
        var nodes=ztree.getSelectedNodes();
        if(nodes.length<=0){
            layer.alert("请选择父级菜单");
            return
        }
        document.getElementById("busForm").reset()
        $("#busForm select[name='type']").val("2");
        $("#treeInfo").show();
        $("#parentName").val(nodes[0].name);
        $("#parentId").val(nodes[0].id);
        $("#id").val("");
    }

    /**
     * 保存或者更新
     */
    $("#submitBtn").click(function () {
        var id = $("#busForm input[name='id']").val();
        var url = "${appPath}/act/bus/edit";
        $.post(url,$("#busForm").serialize(),function (result) {
            if(result.code =='0'){
                var nodes = ztree.getSelectedNodes();
                var bus = result.bus;
                if(id =='' || id == null){
                    //保存
                    toast(result.msg,function(){
                        ztree.addNodes(nodes[0],{id:bus.id,parentId:bus.parentId,name:bus.name});
                    });
                }else {
                    //更新
                    toast(result.msg,function(){
                        nodes[0].name=bus.name;
                        ztree.updateNode(nodes[0]);
                    });
                }

            }else {
                layer.alert(result.msg);
            }
        });
    });

    /**
     * 删除
     */
    function treeDel() {
        debugger
        var nodes = ztree.getSelectedNodes();
        if(nodes == null || nodes.length<1){
            layer.alert("请选择要删除的节点");
            return;
        }
        swal({
            title: "提示",
            text: "您确定要删除该信息么，请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确定",
            closeOnConfirm: false,
            cancelButtonText: "取消",
        }, function () {
            var nodes = ztree.getSelectedNodes();
            var url = "${appPath}/act/bus/del";
            $.ajax({
                url : url,
                type : 'post',
                data : {
                    id : nodes[0].id
                },
                cache : false,
                success : function(d) {
                    if (d.ret==0) {
                        var msg = d.msg;
                        swal("提示！", msg, "success");
                        ztree.removeNode(nodes[0]);
                    }else{
                        var msg = d.msg;
                        swal("提示！", msg, "error");
                    }
                }
            });
        });
        <%--confirm("确定删除吗",function () {--%>
            <%--var nodes = ztree.getSelectedNodes();--%>

            <%--var url = "${appPath}/act/bus/del";--%>
            <%--$.post(url,"id="+nodes[0].id,function (result) {--%>
                <%--if(result.code =='0'){--%>
                    <%--toast(result.msg,function(){--%>
                        <%--ztree.removeNode(nodes[0]);--%>
                    <%--});--%>
                <%--}else {--%>
                    <%--layer.alert(result.msg);--%>
                <%--}--%>
            <%--});--%>
        <%--});--%>
    }
</script>
</body>
</html>

